#
# This script file creates a snapshot (collects values) of rARC database
#

#!/bin/sh

# NOTE! SET THESE VARIABLES
USERNAME_DATABASE=""
PASSWORD_DATABASE=""
NAME_DATABASE=""

GB="1073741824"
DATE=`date +%Y\-%m\-%d`

#This returns the number of contributors
NUMBER_CONTRIBUTES=`mysql -u$USERNAME_DATABASE -p$PASSWORD_DATABASE -D $NAME_DATABASE -e "SELECT count(1) FROM User,Identifier WHERE User.email=Identifier.email AND 
Identifier.offeredSpace AND Identifier.lastUpdate > (SELECT DATE_SUB(curdate(), INTERVAL 1 MONTH))" | egrep "^[0-9]"`

#This returns the total space offered by the contributors
TOTAL_SPACE_OFFERED=`mysql -u$USERNAME_DATABASE -p$PASSWORD_DATABASE -D $NAME_DATABASE -e "select sum(offeredSpace)/($GB) from User,Identifier where User.email=Identifier.email and Identifier.lastUpdate > (SELECT DATE_SUB(curdate(),INTERVAL 1 MONTH));" | egrep "^[0-9]"`

#This returns the total space used by the rARC
TOTAL_SPACE_CORRECT=`mysql -u$USERNAME_DATABASE -p$PASSWORD_DATABASE -D $NAME_DATABASE -e "select sum(usedSpace)/($GB) from Identifier where lastUpdate >= (select curdate());" | egrep "^[0-9]"`

#This saves the values
QUERY="call insertStatisticalRegister('"$DATE"',"$NUMBER_CONTRIBUTES","$TOTAL_SPACE_OFFERED","$TOTAL_SPACE_CORRECT")"
`mysql -u$USERNAME_DATABASE -p$PASSWORD_DATABASE -D $NAME_DATABASE -e "$QUERY"`
